#pragma once
#include <iostream>
#include "Protocol.hpp"

enum
{
    Div_Zero = 1,
    Mod_Zero,
    Other_Op,
};
class ServerCal
{
public:
    ServerCal()
    {
    }

    Response CalHelper(const Request &req)
    {
        Response tmp(0, 0);
        switch (req.op)
        {
        case '+':
            tmp.result = req.x + req.y;
            break;
        case '-':
            tmp.result = req.x - req.y;
            break;
        case '*':
            tmp.result = req.x * req.y;
            break;
        case '/':
        {
            if (req.y == 0)
                tmp.code = Div_Zero;
            else
                tmp.result = req.x / req.y;
        }
        break;
        case '%':
        {
            if (req.y == 0)
                tmp.code=Mod_Zero;
            else
                tmp.result = req.x % req.y;
        }
        break;

        default:
        //其他的操作符暂时无法处理
            tmp.code=Other_Op;
            break;
        }
        return tmp;
    }

    // package为从网上读取的信息流
    std::string Calculator(std::string &package)
    {
        std::string content;
        bool r = Decode(package, &content);
        if (!r)
            return "";
        Request req;
        r = req.Deseralize(content);
        if (!r)
            return "";

        content = "";
        Response resp = CalHelper(req);
        resp.Serialize(&content);
        content = Encode(content);
        return content;
    }

    ~ServerCal()
    {
    }
};